package com.comp3111.happymeal;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.user.JSONParser;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegisterActivity_test_ extends Activity {

	
	// Progress Dialog
    private ProgressDialog pDialog;
 
    JSONParser jsonParser = new JSONParser();
    EditText usernameInput;
    EditText passwordInput;
    EditText emailInput;
    EditText phoneInput;

	private Context context;
    
 
    // url to create new product
    private static String url_register = "http://175.159.102.179/hm/index.php/login/accountRegister";
 
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
//    	success: true
//    private static final String TAG_USERID = "userID";
//    userID: "2"
//    private static final String TAG_EMAIL = "email";
//    email: "ccc"
//    private static final String TAG_PHONE = "phoneNumber";
//    	phoneNumber: "cccs"
//    private static final String TAG_UPDATETIME = "updateTime";
//    	updateTime: "2014-03-13 17:18:31"
 
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); //default
		//setContentView(R.layout.activity_register); //default
		context = this;
		
		//Ryan:
		//usernameInput = (EditText) findViewById(R.id.usernameInput);
		//passwordInput = (EditText) findViewById(R.id.passwordInput);
		//emailInput = (EditText) findViewById(R.id.emailInput);
		//phoneInput = (EditText) findViewById(R.id.phoneInput);
		
		//Button registerButton = (Button) findViewById(R.id.registerButton);
		
		//Ryan: addListener
		//registerButton.setOnClickListener(new View.OnClickListener() {
 
           // @Override
            //public void onClick(View view) {
                // creating new product in background thread
               // new registerAccount().execute();
           // }
       // });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}
	
	   /**
     * Background Async Task to Create new product
     * */
    class registerAccount extends AsyncTask<String, String, String> {
 
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(RegisterActivity_test_.this);
            pDialog.setMessage("Creating an User Account...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
 
        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String usernameText = usernameInput.getText().toString();
            String passwordText = passwordInput.getText().toString();
            String emailText = emailInput.getText().toString();
            String phoneText = phoneInput.getText().toString();
            
            Log.i("Test", "My Message: " + usernameText);
            Log.i("Test", "My Message: " + passwordText);
            Log.i("Test", "My Message: " + emailText);
            Log.i("Test", "My Message: " + phoneText);
            

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", usernameText));
            params.add(new BasicNameValuePair("password", passwordText));
            params.add(new BasicNameValuePair("email", emailText));
            params.add(new BasicNameValuePair("phoneNumber", phoneText));
            
 
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_register, "POST", params);
 
            // check log cat fro response
            Log.i("Create Response", json.toString()); //for debug console 

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
 
                if (success == 1) {
                    // successfully created product
                   // Intent i = new Intent(context, FullscreenActivity.class); //not yet finished!
                   // startActivity(i);
//                	AlertDialog.Builder testAlert = new AlertDialog.Builder(context, R.layout.alert_prompt);
//                	testAlert.create().show();
//                	AlertDialog.setContentView(R.layout.alert_prompt);
                	
                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
 
            return null;
        }
 
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }
 
    }
	
}
